home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / vcommon / RenderInfo.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  6KB  |  207 lines

  1. /*
  2.  * MKSoft Development Amiga ToolKit V1.0
  3.  *
  4.  * Copyright (c) 1985,86,87,88,89,90 by MKSoft Development
  5.  *
  6.  * $Id: RenderInfo.c,v 30.8 1994/08/18 05:53:13 dice Exp dice $
  7.  *
  8.  * $Source: /home/dice/com/src/vcommon/RCS/RenderInfo.c,v $
  9.  *
  10.  * $Date: 1994/08/18 05:53:13 $
  11.  *
  12.  * $Revision: 30.8 $
  13.  *
  14.  * $Log: RenderInfo.c,v $
  15.  * Revision 30.8  1994/08/18  05:53:13  dice
  16.  * .
  17.  *
  18.  * Revision 30.0  1994/06/10  18:09:38  dice
  19.  * .
  20.  *
  21.  * Revision 30.0  1994/06/10  18:09:38  dice
  22.  * .
  23.  *
  24.  * Revision 1.1  1992/07/18  23:07:37  jtoebes
  25.  * Initial revision
  26.  *
  27.  * Revision 1.2  92/05/04  22:31:22  jtoebes
  28.  * First mostly working version.  Still needs list gadgets and menu support
  29.  * 
  30.  * Revision 1.1  92/04/21  06:21:17  J_Toebes
  31.  * Initial revision
  32.  *
  33.  * Revision 1.3  90/06/03  10:25:59  mks
  34.  * Fixed the fact the bottom value was set wrong!
  35.  *
  36.  * Revision 1.2  90/05/20  12:17:03  mks
  37.  * New functions to allocate and free RenderInfo structure.
  38.  * These should be used insted of the old FillIn_RenderInfo...
  39.  * Now has a TextAttr in the RenderInfo
  40.  * Now has the screen Width/Height in RenderInfo
  41.  * Now can be passed a screen pointer
  42.  *
  43.  * Revision 1.1  90/05/09  21:45:02  mks
  44.  * Source now fully under RCS...
  45.  *
  46.  */
  47.  
  48. /*
  49.  * This file contains the definition of the rendering information
  50.  * for elements on the screen.  This information is used to generate
  51.  * the correct pen colours for items on the screen...
  52.  */
  53.  
  54. #include   <exec/types.h>
  55. #include   <exec/memory.h>
  56. #include   <graphics/view.h>
  57. #include   <graphics/text.h>
  58. #include   <intuition/intuition.h>
  59. #include   <intuition/screens.h>
  60.  
  61. #include   <proto/exec.h>
  62. #include   <proto/intuition.h>
  63. #include   <proto/graphics.h>
  64.  
  65. #include   "RenderInfo.h"
  66. #include   "DefaultFonts.h"
  67.  
  68. /*
  69.  * These define the amount of Red, Green, and Blue scaling used
  70.  * to help take into account the different visual impact of those
  71.  * colours on the screen.
  72.  */
  73. #define BLUE_SCALE      2
  74. #define GREEN_SCALE     6
  75. #define RED_SCALE       3
  76.  
  77. /*
  78.  * This returns the colour difference hamming value...
  79.  */
  80. SHORT ColourDifference(UWORD rgb0, UWORD rgb1)
  81. {
  82.    register   SHORT   level;
  83.    register   SHORT   tmp;
  84.  
  85.    tmp=(rgb0 & 15) - (rgb1 & 15);
  86.    level=tmp*tmp*BLUE_SCALE;
  87.    tmp=((rgb0>>4) & 15) - ((rgb1>>4) & 15);
  88.    level+=tmp*tmp*GREEN_SCALE;
  89.    tmp=((rgb0>>8) & 15) - ((rgb1>>8) & 15);
  90.    level+=tmp*tmp*RED_SCALE;
  91.    return(level);
  92. }
  93. #define MAX_COLOURS     16
  94.  
  95. /*
  96.  * For new programs, this also opens fonts...
  97.  */
  98. void FillIn_RenderInfo(struct RenderInfo *ri, struct Screen *TheScreen)
  99. {
  100. register   SHORT      numcolours;
  101. register   SHORT      loop;
  102. register   SHORT      loop1;
  103. register   SHORT      backpen;
  104. register   SHORT      tmp;
  105.            SHORT      colours[16];
  106.            SHORT      colourlevels[16];
  107.            SHORT      pens[16];
  108.    struct  Screen     screen;
  109.  
  110.    if (!TheScreen) GetScreenData((APTR)(TheScreen=&screen),
  111.                                  sizeof(struct Screen),WBENCHSCREEN,NULL);
  112.  
  113.    ri->WindowTop    = TheScreen->WBorTop;
  114.    ri->WindowLeft   = TheScreen->WBorLeft;
  115.    ri->WindowRight  = TheScreen->WBorRight;
  116.    ri->WindowBottom = TheScreen->WBorBottom;
  117.    ri->WindowTitle  = TheScreen->BarHeight-TheScreen->BarVBorder+TheScreen->WBorTop;
  118.  
  119.    ri->ScreenWidth  = TheScreen->Width;
  120.    ri->ScreenHeight = TheScreen->Height;
  121.  
  122.    ri->FontSize     = TheScreen->Font->ta_YSize;
  123.  
  124.    if (!(ri->TheFont=OpenFont(TheScreen->Font)))
  125.       ri->TheFont = OpenFont(&TOPAZ80);
  126.  
  127.    if (ri->TheFont)
  128.    {
  129.       ri->TextAttr.ta_Name=ri->TheFont->tf_Message.mn_Node.ln_Name;
  130.       ri->TextAttr.ta_YSize=ri->TheFont->tf_YSize;
  131.       ri->TextAttr.ta_Style=ri->TheFont->tf_Style;
  132.       ri->TextAttr.ta_Flags=ri->TheFont->tf_Flags;
  133.    }
  134.    else ri->TextAttr=TOPAZ80;
  135.  
  136.    numcolours=1 << (TheScreen->RastPort.BitMap->Depth);
  137.    if (numcolours>16) numcolours=16;
  138.  
  139.    if (numcolours<3)
  140.    {       /* Some silly person is running with 2 colours... */
  141.       ri->BackPen=0;
  142.       ri->Highlight=1;
  143.       ri->Shadow=1;
  144.       ri->TextPen=1;
  145.    }
  146.    else
  147.    {
  148.       for (loop=0;loop<numcolours;loop++)
  149.       {
  150.          colours[loop]=GetRGB4(TheScreen->ViewPort.ColorMap,(LONG)loop);
  151.          colourlevels[loop]=ColorLevel(colours[loop]);
  152.          pens[loop]=loop;
  153.       }
  154.  
  155.       /* Sort darkest to brightest... */
  156.       for (loop=0;loop<(numcolours-1);loop++)
  157.          for (loop1=loop+1;loop1<numcolours;loop1++)
  158.          {
  159.             if (colourlevels[loop]>colourlevels[loop1])
  160.             {
  161.                tmp=colourlevels[loop];
  162.                colourlevels[loop]=colourlevels[loop1];
  163.                colourlevels[loop1]=tmp;
  164.  
  165.                tmp=colours[loop];
  166.                colours[loop]=colours[loop1];
  167.                colours[loop1]=tmp;
  168.  
  169.                tmp=pens[loop];
  170.                pens[loop]=pens[loop1];
  171.                pens[loop1]=tmp;
  172.             }
  173.           }
  174.  
  175.       /* Now, pick the pens... HightLight... */
  176.       loop=numcolours-1;
  177.       while (!(ri->Highlight=pens[loop--]));
  178.  
  179.       /* and Shadow... */
  180.       loop=0;
  181.       while (!(ri->Shadow=pens[loop++]));
  182.  
  183.       /* The BackGround pen... */
  184.       if (!pens[loop]) loop++;
  185.       ri->BackPen=pens[backpen=loop];
  186.  
  187.       loop1=0;
  188.       for (loop=0;loop<numcolours;loop++)
  189.       {
  190.          tmp=ColourDifference(colours[loop],colours[backpen]);
  191.          if (tmp > loop1)
  192.          {
  193.             loop1 = tmp;
  194.             ri->TextPen = pens[loop];
  195.          }
  196.       }
  197.    }
  198. }
  199.  
  200. /*
  201.  * Close the font and free the memory...
  202.  */
  203. VOID CleanUp_RenderInfo(struct RenderInfo *ri)
  204. {
  205.    if (ri && ri->TheFont) CloseFont(ri->TheFont);
  206. }
  207.